<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>订单管理</el-breadcrumb-item>
            <el-breadcrumb-item>订单列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片信息 -->
        <el-card>
            <!-- 订单搜索区 -->
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容"  class="input-with-select" clearable>
                        <el-button slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
            </el-row>
            <!-- 订单表单区 -->
            <el-table :data="ordersList" border stripe>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="订单编号" prop="order_number" width="400px"></el-table-column>
                <el-table-column label="订单价格" prop="order_price" align="center"></el-table-column>
                <el-table-column label="是否付款" prop="order_pay" align="center">
                  <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.pay_status === '1' ">已付款</el-tag>
                    <el-tag type="danger" v-else>未付款</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="是否发货" prop="is_send" align="center"></el-table-column>
                <el-table-column label="下单时间" prop="create_time" width="200px">
                    <template slot-scope="scope">
                        {{scope.row.create_time|dateFormat}}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="200px" align="center">
                    <template>
                        <!-- 修改订单信息 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini" @click="modifyDialogVisible = true"></el-button>
                        <!-- 删除用户 -->
                        <el-button type="success" icon="el-icon-location" size="mini" @click="getLogisticsInfo"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页栏 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="orderListQuery.pagenum" :page-sizes="[6,8, 10,12]" :page-size="orderListQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="orderTotal">
            </el-pagination>
        </el-card>

        <!-- 修改地址对话框 -->
        <el-dialog title="修改订单" :visible.sync="modifyDialogVisible" width="50%" @close="emptyAddressForm">
          <el-form :model="addressForm" :rules="addressFormRules" ref="addressFormRef" label-width="100px" class="demo-ruleForm">
            <el-form-item label="省市区/县" prop="addressInfo">
              <el-cascader v-model="addressForm.addressInfo" :options="cityDate" :props="addressCascaderProps" class="cascader_style" clearable></el-cascader>
            </el-form-item>
            <el-form-item label="详细地址" prop="detailedAddress">
              <el-input v-model="addressForm.detailedAddress"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="modifyDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="modifyDialogVisible = false">确 定</el-button>
          </span>
        </el-dialog>
        <!-- 物流信息对话框 -->
        <el-dialog title="物流信息" :visible.sync="logisticsDialogVisible" width="50%">
          <el-timeline>
            <el-timeline-item v-for="(activity, index) in logisticsList" :key="index" :timestamp="activity.time">
                {{activity.context}}
            </el-timeline-item>
          </el-timeline>
        </el-dialog>
    </div>
</template>

<script>
import cityDate from './citydata'
export default {
  data() {
    return {
      // 订单列表配置
      orderListQuery: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      // 订单列表
      ordersList: [],
      // 订单总条数
      orderTotal: 0,
      // 修改订单城市级联选择框数据
      cityDate,
      // 是否显示修改地址对话框
      modifyDialogVisible: false,
      // 修改地址表单
      addressForm: {
        addressInfo: [],
        detailedAddress: ''
      },
      // 修改地址表单验证规则
      addressFormRules: {
        addressInfo: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        detailedAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      // 级联选择框配置对象
      addressCascaderProps: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children'
      },
      // 是否显示物流信息对话框
      logisticsDialogVisible: false,
      // 物流信息列表
      logisticsList: []
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    //   获取订单列表
    async getOrdersList() {
      const { data: res } = await this.$http.get('/orders', { params: this.orderListQuery })
      if (res.meta.status !== 200) {
        return this.$message({
          showClose: true,
          message: res.meta.msg,
          type: 'error'
        })
      }
      console.log(res.data)
      this.ordersList = res.data.goods
      this.orderTotal = res.data.total
    },
    // 监听每页有多少条订单信息
    handleSizeChange(newPagesize) {
      this.orderListQuery.pagesize = newPagesize
      this.getOrdersList()
    },
    // 监听当前在第几页
    handleCurrentChange(nePagenum) {
      this.orderListQuery.pagenum = nePagenum
      this.getOrdersList()
    },
    // 关闭对话框时
    emptyAddressForm() {
      this.$refs.addressFormRef.resetFields()
    },
    // 点击查询物流信息
    async getLogisticsInfo() {
      // const { data: res } = await this.$http.get('/kuaidi/804909574412544580')
      // if (res.meta.status !== 200) {
      //   return this.$message({
      //     showClose: true,
      //     message: '获取物流信息失败！',
      //     type: 'error'
      //   })
      // }
      // this.logisticsList = res.data
      // console.log(this.logisticsList)
      this.logisticsDialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.el-table,.el-pagination{
    margin-top: 20px;
}
.cascader_style {
  width: 100%;
}
</style>
